package com.jader.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.activities.ShopCartActivity;
import com.jader.activities.ShopCartActivity.OnCheckAllListener;
import com.jader.activities.Store;
import com.jader.beans.ShopCartGoods;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

public class ShopCartAdapter extends BaseExpandableListAdapter implements
		OnCheckAllListener {

	Context context;
	List<String> parent_sellerName = new ArrayList<String>();
	Map<String, List<ShopCartGoods>> child_goods = new HashMap<String, List<ShopCartGoods>>();
	LayoutInflater inflater;
	private OnShopCartChildClickListener onShopCartChildClickListener;
	private Map<String, CheckBox> parent_CheckBox = new HashMap<String, CheckBox>();
	private Map<String, List<CheckBox>> child_CheckBox_map = new HashMap<String, List<CheckBox>>();
	private OnShopCartChildCheckChangedListener childCheckChangedListener;
	private OnShopCartGroupCheckChangedListener groupCheckChangedListener;
	private List<CheckBox> child_CheckBox_list = null;

	public ShopCartAdapter(List<String> list,
			Map<String, List<ShopCartGoods>> map, Context context) {
		super();
		ShopCartActivity activity = (ShopCartActivity) context;
		activity.setOnCheckAllListener(this);
		this.parent_sellerName = list;
		this.child_goods = map;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	// 分组个数
	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		Log.e("parentCount", "" + parent_sellerName.size());
		return parent_sellerName.size();
	}

	// 获取当前父item下的子item的个数
	@Override
	public int getChildrenCount(int groupPosition) {
		String childSize = parent_sellerName.get(groupPosition);
		int childCount = child_goods.get(childSize).size();
		Log.e("childCount", "" + childCount);
		return childCount;
	}

	// 获取当前父item的数据
	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return parent_sellerName.get(groupPosition);
	}

	// 得到子item需要关联的数据
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		String Child = parent_sellerName.get(groupPosition);
		return (child_goods.get(Child).get(childPosition));
	}

	// 得到父item的ID
	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	// 得到子item的ID
	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	// 设置父item组件
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.shopcart_parent, null);
		}
		convertView.setTag(R.id.tv_parent_sellername, groupPosition);
		convertView.setTag(R.id.tv_child_goodname, -1);
		TextView tv_parent_sellerName = (TextView) convertView
				.findViewById(R.id.tv_parent_sellername);
		// CheckBox CB_parent_goodSelect = (CheckBox) convertView
		// .findViewById(R.id.check_parent_goodSelect);
		CheckBox CB_parent_goodSelect = (CheckBox) convertView
				.findViewById(R.id.check_parent_goodSelect);
		String sellerName = this.parent_sellerName.get(groupPosition);
		tv_parent_sellerName.setText(sellerName);

		Log.e("parent_list", sellerName);
		groupCheckChangedListener = new OnShopCartGroupCheckChangedListener(
				groupPosition);
		CB_parent_goodSelect.setFocusable(false);
		CB_parent_goodSelect
				.setOnCheckedChangeListener(groupCheckChangedListener);
		Set<String> key_group = parent_CheckBox.keySet();
		boolean flag_group = true;
		Iterator<String> iterator_group = key_group.iterator();
		while (iterator_group.hasNext()) {
			String text = iterator_group.next();
			if (sellerName.equals(text)) {
				flag_group = false;
			}
		}
		if (flag_group) {
			parent_CheckBox.put(sellerName, CB_parent_goodSelect);
		}
		OnGroupClickListener onGroupClickListener = new OnGroupClickListener(
				groupPosition);
		tv_parent_sellerName.setOnClickListener(onGroupClickListener);
		tv_parent_sellerName.setFocusable(false);
		child_CheckBox_list = new ArrayList<CheckBox>();
		Set<String> key = child_CheckBox_map.keySet();
		Iterator<String> iterator = key.iterator();
		boolean flag = true;
		while (iterator.hasNext()) {
			String text = iterator.next();
			if (sellerName.equals(text)) {
				flag = false;
			}
		}
		if (flag) {
			child_CheckBox_map.put(sellerName, child_CheckBox_list);
		}
		return convertView;

	}

	// 设置子item组件
	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.shopcart_child, null);
		}
		convertView.setTag(R.id.tv_parent_sellername, groupPosition);
		convertView.setTag(R.id.tv_child_goodname, childPosition);
		String childView = this.parent_sellerName.get(groupPosition);
		final ShopCartGoods info = child_goods.get(childView)
				.get(childPosition);
		// CheckBox CB_child_goodSelect = (CheckBox) convertView
		// .findViewById(R.id.check_child_goodSelect);
		final CheckBox CB_child_goodSelect = (CheckBox) convertView
				.findViewById(R.id.check_child_goodSelect);
		TextView tv_child_goodname = (TextView) convertView
				.findViewById(R.id.tv_child_goodname);
		TextView tv_child_goodDesc = (TextView) convertView
				.findViewById(R.id.tv_child_description);
		final TextView tv_child_goodprice = (TextView) convertView
				.findViewById(R.id.tv_child_goodprice);
		final TextView tv_child_goodNum = (TextView) convertView
				.findViewById(R.id.edit_child_goodNum);
		ImageView img_child_goodpic = (ImageView) convertView
				.findViewById(R.id.img_child_goodPic);

		TextView tv_child_plus = (TextView) convertView
				.findViewById(R.id.tv_child_plus);
		TextView tv_child_minus = (TextView) convertView
				.findViewById(R.id.tv_child_minus);

		JaderApplication.getBitmapUtils().display(img_child_goodpic,
				JaderApplication.getIP()+"/Jader" + info.getGoods().getPicture());
		String goodName = info.getGoods().getGoodsName();
		final double goodPrice = info.getGoods().getPrice();
		int goodNum = info.getShopcart().getNumber();
		String goodDesc = info.getGoods().getDesc();
		tv_child_goodname.setText(goodName);
		tv_child_goodDesc.setText(goodDesc);
		tv_child_goodNum.setText("" + goodNum);
		tv_child_goodprice.setText("￥" + goodNum * goodPrice);
		// 单击事件
		tv_child_plus.setFocusable(false);
		tv_child_plus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int P_number = Integer.parseInt(tv_child_goodNum.getText()
						.toString().trim());
				Log.e("stock", info.getGoods().getStock() + "");
				if (P_number < info.getGoods().getStock()) {
					P_number += 1;
				}

				tv_child_goodprice.setText("￥" + P_number * goodPrice);
				tv_child_goodNum.setText("" + P_number);
				info.getShopcart().setNumber(P_number);
				onShopCartChildClickListener.onShopCartChildClickListener(info,
						CB_child_goodSelect.isChecked());
			}
		});
		tv_child_minus.setFocusable(false);
		tv_child_minus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				int M_number = Integer.parseInt(tv_child_goodNum.getText()
						.toString().trim());
				if (M_number > 1) {
					M_number -= 1;
					tv_child_goodprice.setText("￥" + M_number * goodPrice);
					tv_child_goodNum.setText("" + M_number);
				} else {
					M_number = 1;
				}
				info.getShopcart().setNumber(M_number);
				onShopCartChildClickListener.onShopCartChildClickListener(info,
						CB_child_goodSelect.isChecked());
			}
		});
		childCheckChangedListener = new OnShopCartChildCheckChangedListener(
				groupPosition, childPosition);
		CB_child_goodSelect.setFocusable(false);
		CB_child_goodSelect
				.setOnCheckedChangeListener(childCheckChangedListener);
		child_CheckBox_list = child_CheckBox_map.get(childView);
		boolean flag = true;
		for (int i = 0; i < child_CheckBox_list.size(); i++) {
			if (CB_child_goodSelect.equals(child_CheckBox_list.get(i))) {
				flag = false;
			}
		}
		if (flag) {
			child_CheckBox_list.add(CB_child_goodSelect);
		}
		Log.e("child", childView + "-->" + goodName);
		CB_child_goodSelect.setChecked(parent_CheckBox.get(
				info.getSeller().getSellername()).isChecked());
		// child_CheckBox_list.add(CB_child_goodSelect);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	public class OnShopCartGroupCheckChangedListener implements
			OnCheckedChangeListener {

		private int groupPosition;

		public OnShopCartGroupCheckChangedListener(int groupPosition) {
			this.groupPosition = groupPosition;
		}

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			String seller = ShopCartAdapter.this.parent_sellerName
					.get(groupPosition);
			List<CheckBox> checkBoxs = child_CheckBox_map.get(seller);
			for (CheckBox check : checkBoxs) {

				check.setChecked(arg1);
			}

		}

	}

	public class OnShopCartChildCheckChangedListener implements
			OnCheckedChangeListener {

		private int groupPosition;
		private int childPosition;

		public OnShopCartChildCheckChangedListener(int groupPosition,
				int childPosition) {
			this.groupPosition = groupPosition;
			this.childPosition = childPosition;
		}

		@Override
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			String seller = ShopCartAdapter.this.parent_sellerName
					.get(groupPosition);
			ShopCartGoods shopCartGoods = child_goods.get(seller).get(
					childPosition);
			onShopCartChildClickListener.onShopCartChildClickListener(
					shopCartGoods, arg1);

		}
	}

	public interface OnShopCartChildClickListener {
		void onShopCartChildClickListener(ShopCartGoods shopCartGoods,
				boolean isChecked);
	}

	public OnShopCartChildClickListener getOnShopCartChildClickListener() {
		return onShopCartChildClickListener;
	}

	public void setOnShopCartChildClickListener(
			OnShopCartChildClickListener onShopCartChildClickListener) {
		this.onShopCartChildClickListener = onShopCartChildClickListener;
	}

	@Override
	public void onCheckAllListener(boolean isChecked) {
		for (String sellername : parent_sellerName) {
			parent_CheckBox.get(sellername).setChecked(isChecked);
			List<CheckBox> list = child_CheckBox_map.get(sellername);
			for (CheckBox checkBox : list) {
				checkBox.setChecked(isChecked);
			}
		}

	}

	class OnGroupClickListener implements OnClickListener {

		int groupPosition;

		public OnGroupClickListener(int groupPosigion) {
			this.groupPosition = groupPosigion;
		}

		@Override
		public void onClick(View arg0) {
			Intent intent = new Intent(context, Store.class);
			intent.putExtra("sellerid",
					child_goods.get(parent_sellerName.get(groupPosition))
							.get(0).getSeller().getSellerid());
			Activity activity = (Activity) context;
			activity.startActivity(intent);
		}

	}

}
